import java.util.LinkedList;
import java.util.Queue;
//栈的进入为push出为pop仅查看为top
//队列进入为offer出为poll仅查看为peek
public class MyStack {  //两个队列实现一个栈的先入后出
    private Queue<Integer> queue1;
    private Queue<Integer> queue2;
    public MyStack(){
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }
    public void push(int x){ //如果第一个队列为空则加入第一个队列，第二个空加第二个，都为空加第一个
        if (!queue1.isEmpty()){
            queue1.offer(x);
        }else if (!queue2.isEmpty()){
            queue2.offer(x);
        }else {
            queue1.offer(x);
        }
    }
    public int pop(){
        if (empty()){
            return -1;//两个队列都为空则栈为空
        }
        if (!queue1.isEmpty()){
            int size = queue1.size();
            for (int i = 0; i < size-1; i++) {
                int tmp = queue1.poll();
                queue2.offer(tmp);
            }
            return queue1.poll();
        }
        else{
            int size = queue2.size();
            for (int i = 0; i < size-1; i++) {
                int tmp = queue2.poll();
                queue1.offer(tmp);
            }
            return queue2.poll();
        }
    }
    public int top(){ //peek
        if (empty()){
            return -1;//两个队列都为空则栈为空
        }
        int tmp = -1;
        if (!queue1.isEmpty()){
            int size = queue1.size();
            for (int i = 0; i < size; i++) {
                tmp = queue1.poll();
                queue2.offer(tmp);
            }
            return tmp;
        }
        else{
            int size = queue2.size();
            for (int i = 0; i < size-1; i++) {
                tmp = queue2.poll();
                queue1.offer(tmp);
            }
            return tmp;
        }
    }
    public boolean empty(){
        return queue1.isEmpty() && queue2.isEmpty();
    }
}
